package com.itheima.web.servlet;

import cn.hutool.core.util.IdUtil;
import com.itheima.domain.*;
import com.itheima.factory.BeanFactory;
import com.itheima.service.AddressService;
import com.itheima.service.OrderService;
import com.itheima.util.CartUtils;
import com.itheima.util.JedisUtils;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/OrderServlet")
public class OrderServlet extends BaseServlet{
    AddressService addressService = (AddressService) BeanFactory.getBean("addressService");
    //作为模板使用
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    //生成订单
    protected void createOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Date orderDate=new Date();
        //1.接受请求参数
        String addressId=request.getParameter("addressId");
        String[] addressArray = addressId.split(",");
        //2.获取session中的user
        User currentUser = (User) request.getSession().getAttribute("currentUser");
        //3.获取redis中的cart
        Cart cart = CartUtils.getCartForRedis(currentUser);

        //4.创建订单，设置基础数据
        Order order = new Order();
        order.setOid(IdUtil.simpleUUID()); // 订单号
        order.setOrdertime(orderDate); // 下单时间
        order.setTotal(cart.getCartTotal()); // 订单总金额
        order.setState(0); // 未支付订单...
        order.setContact(addressArray[0]); // 收货人
        order.setAddress(addressArray[1]);// 收货地址
        order.setTelephone(addressArray[2]);// 收货电话
        // order.setUser(currentUser); // 所属用户
        order.setUid(currentUser.getUid()); // 为了简化所属用户操作，我们可以直接设置uid
        //5.遍历购物项，创建订单项
        List<OrderItem>orderItemList=new ArrayList<>();
        OrderItem orderItem=null;
        //遍历购物项
        for (CartItem cartItem : cart.getCartItemMap().values()) {
            // 创建订单项
            orderItem = new OrderItem();
            orderItem.setItemtime(orderDate); // 下单时间
            orderItem.setState(0); // 未支付
            orderItem.setNum(cartItem.getNum());// 数量
            orderItem.setSubtotal(cartItem.getSubTotal()); // 商品小计
            orderItem.setRid(cartItem.getRoute().getRid()); // 商品rid ，简化数据库操作
            orderItem.setOid(order.getOid());// 订单oid，简化数据操作
            //添加到订单项集合中
            orderItemList.add(orderItem);
        }
        // 将订单项集合关联到订单实体中
             order.setOrderItemList(orderItemList);
             //6.调用service保存到数据库
        OrderService orderService = (OrderService) BeanFactory.getBean("orderService");
        orderService.save(order);
        //7.清空购物车
        Jedis jedis = JedisUtils.getJedis();
        jedis.del("cart_"+currentUser.getUsername());
        jedis.close();
        //8.8.重定向支付页面
        response.sendRedirect(request.getContextPath()+"/pay.jsp");


    }
    protected void orderInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     //1.从session中获取user对象
        User currentUser = (User) request.getSession().getAttribute("currentUser");
        //2.调用addressService查询收货地址列表
        List<Address> addressList =addressService.findByUid(currentUser.getUid());
        //3.查询购物车redis中
        Cart cart = CartUtils.getCartForRedis(currentUser);
        //4.将数据设置到request域中
        request.setAttribute("addressList",addressList);
        request.setAttribute("cart",cart);
        //5.转发到order_info.jsp
        request.getRequestDispatcher("/order_info.jsp").forward(request,response);

    }
}
